package com.star.project.cococo.util;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jmessage.api.JMessageClient;
import cn.jmessage.api.chatroom.ChatRoomListResult;
import cn.jmessage.api.chatroom.CreateChatRoomResult;
import cn.jmessage.api.common.model.RegisterInfo;
import cn.jmessage.api.common.model.UserPayload;
import cn.jmessage.api.common.model.chatroom.ChatRoomPayload;

public class IMUtils {
    private final static String appkey = "4aa95da6ddec3b81b43f3515";
    private final static String masterSecret = "2ecd444d6fde142fa4e0f36e";
    private final static JMessageClient client = new JMessageClient(appkey, masterSecret);

    /**
     * 用户注册
     */
    public static void registerUsers(String userName,String nickName,String avatar) throws APIConnectionException, APIRequestException {
        RegisterInfo registerInfo = RegisterInfo.newBuilder()
                .setUsername(userName).setNickname(nickName)
                .setPassword(userName).setAvatar(avatar)
                .build();
        RegisterInfo[] registerInfos = new RegisterInfo[1];
        registerInfos[0] = registerInfo;

        String string = client.registerUsers(registerInfos);

        System.out.println(string);
    }

    /**
     * 用户更新头像
     */
    public static void updateUsersAvatar(String userName,String avatar) throws APIConnectionException, APIRequestException {
        UserPayload payload =UserPayload.newBuilder().setAvatar(avatar).build();
        client.updateUserInfo(userName,payload);
    }

    /**
     * 用户更新昵称
     */
    public static void updateUsersNickName(String userName,String nickName) throws APIConnectionException, APIRequestException {
        UserPayload payload =UserPayload.newBuilder().setNickname(nickName).build();
        client.updateUserInfo(userName,payload);
    }

    /**
     * 用户禁用
     */
    public static void forbidUser(String username, boolean disable) throws APIConnectionException, APIRequestException {
        client.forbidUser(username,disable);
    }

    /**
     * 用户删除
     */
    public static void deleteUser(String username) throws APIConnectionException, APIRequestException {
        client.deleteUser(username);
    }

    /**
     * 创建聊天室
     */
    public static String createChatRoom(String username,String roomName) throws APIConnectionException, APIRequestException {
        CreateChatRoomResult chatRoom = client.createChatRoom(ChatRoomPayload.newBuilder()
                .setOwnerUsername(username)
                .setName(roomName)
                .build());
        return chatRoom.getChatroom_id().toString();
    }

    /**
     * 获取聊天室详情
     */
    public static ChatRoomListResult getChatroomInfo(String imId) throws APIConnectionException, APIRequestException {
        ChatRoomListResult result = client.getBatchChatRoomInfo(Long.parseLong(imId));
        return result;
    }


    /**
     * 移除聊天室
     */
    public static void removeChatRoomMembers(long roomId,String members) throws APIConnectionException, APIRequestException {
        client.removeChatRoomMembers(roomId, members);
    }


    /**
     * 测试
     */
    public static void test() throws APIConnectionException, APIRequestException {
        CreateChatRoomResult chatRoom = client.createChatRoom(ChatRoomPayload.newBuilder()
                .setOwnerUsername("100010")
                .setName("可可小窝")
                .build());
        System.out.println(chatRoom);
    }


}
